/* eslint-disable */
/**报表用于响应扩展按钮点击事件
 */
const reportAfterBtnFuns = {
  //结算汇总报表
  async settlementSumSp(key, config, data, that) {
    if (key == 'printsp') { //供应商汇总打印
      //必须选择会计区间
      let periodColumn = that.chartModel.gridFields.filter(column => column.field == 'period')[0]
      if (!periodColumn.value) {
        that.utilMessage.warning('进行汇总打印，请先选择会计区间进行筛选')
        $('#period input').focus()
        return
      }

      //查询会计期间
      let wrapper = that.$newWrapper().eq('period', periodColumn.value)
      const params = {
        queryString: utilVue.json2str(wrapper.queryConditions),
        orderString: null
      }
      let periodOb = {}
      await that.$ajax.get('finance/period/list', {
        params
      }).then(resp => {
        if (resp.data) {
          if (resp.data.length == 1) {
            periodOb = resp.data[0]
          } else if (resp.data.length > 1) {
            that.utilMessage.warning('结算周期查询到多个，请联系运营人员进行调整')
          } else {
            that.utilMessage.warning('结算周期已被删除')
          }
        }
      })
      if (!periodOb.id) return

      //按供应商分组
      let arrSupplierName = [];
      data.filter((row) => !arrSupplierName.includes(row.supplier_name) && arrSupplierName.push(row.supplier_name))

      let paramArr = []
      let startDate = that.dates.toDate(periodOb.startDate, 'yyyy-MM-dd')
      let endDate = that.dates.toDate(periodOb.endDate, 'yyyy-MM-dd')
      let subtitle = `会计期间${periodOb.period}，结算期间(${startDate}--${endDate})`
      arrSupplierName.forEach(supplier_name => {
        let queryWrapper = that.getQueryWrapper()
        queryWrapper.eq('supplier_name', supplier_name)
        let param = {
          title: '结算汇总单',
          subtitle: subtitle,
          config: config,
          queryWrapper: queryWrapper,
          form: {},
          head: [{
            name: 'supplier_name',
            text: '供应商',
            value: supplier_name,
            col: 12
          }],
          foot: [{
              name: '1',
              text: '制表日期',
              col: 8
            },
            {
              name: '2',
              text: '制表人',
              col: 8
            },
            {
              name: '3',
              text: '复核人',
              col: 8
            },
          ]
        }
        paramArr.push(param)
      })

      global.utilPrint.openReport(paramArr)
    }
  },
  //科室病人消耗明细报表
  async afterDeptOprtUseDtl(key, config, data, that) {
    if (key == 'printUser') { //打印
      let deptBySpArr = [];
      let allData = await that.getAllData(config.code)
      let data = allData.data
      // data.filter((row) => !execDeptNameArr.includes(row.exec_dept_name) && execDeptNameArr.push(row.exec_dept_name))
      data.filter((row) => {
        let deptUser = {};
        deptUser.execDeptName = row.exec_dept_name
        deptUser.supplierName = row.supplier_name
        deptUser.inpatientNo = row.inpatient_no
        deptUser.patientName = row.patient_name
        deptUser.useDate = row.use_date
        if (!JSON.stringify(deptBySpArr).includes(JSON.stringify(deptUser))) {
          deptBySpArr.push(deptUser)
        }
      })
      let paramArr = []
      deptBySpArr.forEach(item => {
        let execDeptName = item.execDeptName
        let supplierName = item.supplierName
        let inpatientNo = item.inpatientNo
        let patientName = item.patientName
        let useDate = item.useDate
        let queryWrapper = that.getQueryWrapper()
        queryWrapper.eq('exec_dept_name', execDeptName).eq('supplier_name', supplierName)
          .eq('inpatient_no', inpatientNo).eq('patient_name', patientName).eq("use_date", useDate)

        let head = [{
            name: 'execDeptName',
            text: '执行科室',
            value: execDeptName,
            col: 8
          },
          {
            name: 'supplierName',
            text: '供应商',
            value: supplierName,
            col: 8
          },
          {
            name: 'inpatientNo',
            text: '住院编号',
            value: inpatientNo,
            col: 8
          },
          {
            name: 'patientName',
            text: '病人姓名',
            value: patientName,
            col: 4
          },
          {
            name: 'useDate',
            text: '消耗日期',
            value: useDate,
            col: 4
          }
        ]
        // that.cpUserQuery.forEach(item => {
        //   if (that.strings.isBlank(item.value)) {
        //     return true
        //   }
        //   let field = item.field
        //   if (item.queryType === 'numberArea' || item.queryType === 'dateArea') {
        //     head.push({name: item.field, text: item.label,value: item.value+'--'+ item.value1})
        //   } else  {
        //     if (item.field != 'exec_dept_name') {
        //       head.push({name: item.field, text: item.label,value: item.value})
        //     }
        //   }
        // })
        let param = {
          title: global.settings.hospitalName + '科室病人消耗单',
          subtitle: that.chartModel.config.subtitle,
          config: config,
          queryWrapper: queryWrapper,
          form: {},
          head: head,
          foot: [{
              name: '2',
              text: '制单人',
              col: 6,
              value: that.utils.getCurrentUser().name
            },
            {
              name: '3',
              text: '器械科',
              col: 6
            },
            {
              name: '1',
              text: '科室审核',
              col: 6
            },
          ]
        }
        paramArr.push(param)
      })
      global.utilPrint.openReport(paramArr)
    }
    // 按供应商
    if (key == 'printSp') {
      let deptBySpArr = [];
      config.code = "dept_oprt_use_sum_bysp"
      let allData = await that.getAllData(config.code)
      let data = allData.data
      data.filter((row) => {
        let deptUser = {};
        deptUser.supplierName = row.supplier_name
        if (!JSON.stringify(deptBySpArr).includes(JSON.stringify(deptUser))) {
          deptBySpArr.push(deptUser)
        }
      })
      let paramArr = []
      deptBySpArr.forEach(item => {
        let supplierName = item.supplierName
        let queryWrapper = that.getQueryWrapper()
        queryWrapper.eq('supplier_name', supplierName)
        let head = [{
          name: 'supplierName',
          text: '供应商',
          value: supplierName,
          col: 24
        }]
        let param = {
          title: global.settings.hospitalName + '科室病人消耗单',
          subtitle: that.chartModel.config.subtitle,
          config: config,
          queryWrapper: queryWrapper,
          form: {},
          head: head,
          foot: [{
              name: '2',
              text: '制单人',
              col: 6,
              value: that.utils.getCurrentUser().name
            },
            {
              name: '3',
              text: '器械科',
              col: 6
            }
          ]
        }
        paramArr.push(param)
      })
      global.utilPrint.openReport(paramArr)
    }
  },


  //科室病人消耗汇总报表
  async afterDeptOprtUseSumDtl(key, config, data, that) {

    // 按供应商
    if (key == 'printSp') {
      let deptBySpArr = []
      let allData = await that.getAllData(config.code)
      let data = allData.data
      data.filter((row) => {
        let deptUser = {};
        deptUser.supplierName = row.supplier_name
        if (!JSON.stringify(deptBySpArr).includes(JSON.stringify(deptUser))) {
          deptBySpArr.push(deptUser)
        }
      })
      let paramArr = []
      deptBySpArr.forEach(item => {
        let supplierName = item.supplierName
        let queryWrapper = that.getQueryWrapper()
        queryWrapper.eq('supplier_name', supplierName)
        let head = [{
          name: 'supplierName',
          text: '供应商',
          value: supplierName,
          col: 24
        }]
        let param = {
          title: global.settings.hospitalName + '病人消耗单',
          subtitle: that.chartModel.config.subtitle,
          config: config,
          queryWrapper: queryWrapper,
          form: {},
          head: head,
          foot: [{
            name: '2',
            text: '制单人',
            col: 6,
            value: that.utils.getCurrentUser().name
          },
            {
              name: '3',
              text: '器械科',
              col: 6
            }
          ]
        }
        paramArr.push(param)
      })
      global.utilPrint.openReport(paramArr)
    }
  },
  //
  async afterDeptBillHrp(key, config, data, that,selectRow) {
    if (key == 'printCheck') { //打印
      if (selectRow.length===0){
        that.utilMessage.warning('数据为空,请选择数据')
        return
      }
      let deptBySpArr = [];
      let printMap = new Map()
      let printPkArr = []
      // data.filter((row) => !execDeptNameArr.includes(row.exec_dept_name) && execDeptNameArr.push(row.exec_dept_name))
      selectRow.filter((row) => {
        let deptUser = {};
        deptUser.execDeptName = row.exec_dept_name
        deptUser.supplierName = row.supplier_name
        deptUser.inpatientNo = row.inpatient_no
        deptUser.patientName = row.patient_name
        deptUser.useDate = row.use_date
        deptUser.hrpKey = row.hrp_key
        deptUser.flowModifier = row.flow_modifier

        if (!JSON.stringify(deptBySpArr).includes(JSON.stringify(deptUser))) {
          printPkArr = [];
          deptBySpArr.push(deptUser)
          printMap.set(deptUser,printPkArr)
        }
        if (deptBySpArr.length >0 && JSON.stringify(deptBySpArr).includes(JSON.stringify(deptUser))) {
          printPkArr.push(row.id)
        }
      })
      let paramArr = []
      deptBySpArr.forEach((item,index) => {
        let execDeptName = item.execDeptName
        let inpatientNo = item.inpatientNo
        let supplierName = item.supplierName
        let patientName = item.patientName
        let useDate = item.useDate
        let hrpKey = item.hrpKey
        let flowModifier = item.flowModifier
        let queryWrapper = that.getQueryWrapper()
        queryWrapper.eq('exec_dept_name', execDeptName).eq('hrp_key', hrpKey)
          .eq('supplier_name', supplierName)
          .eq('inpatient_no', inpatientNo).eq('patient_name', patientName).eq("use_date", useDate)

        let head = [{
          name: 'execDeptName',
          text: '执行科室',
          value: execDeptName,
          col: 12
        },
          {
            name: 'supplierName',
            text: '供应商',
            value: supplierName,
            col: 12
          },
          {
            name: 'inpatientNo',
            text: '住院编号',
            value: inpatientNo,
            col: 8
          },
          {
            name: 'useDate',
            text: '消耗日期',
            value: useDate,
            col: 7
          },
          {
            name: 'hrpKey',
            text: 'HRP专购品单号',
            value: hrpKey,
            col: 8
          },
          {
            name: 'patientName',
            text: '病人姓名',
            value: patientName,
            col: 8
          },

        ]
        // that.cpUserQuery.forEach(item => {
        //   if (that.strings.isBlank(item.value)) {
        //     return true
        //   }
        //   let field = item.field
        //   if (item.queryType === 'numberArea' || item.queryType === 'dateArea') {
        //     head.push({name: item.field, text: item.label,value: item.value+'--'+ item.value1})
        //   } else  {
        //     if (item.field != 'exec_dept_name') {
        //       head.push({name: item.field, text: item.label,value: item.value})
        //     }
        //   }
        // })
        let param = {
          title: global.settings.hospitalName+'科室病人消耗单',
          subtitle: "",
          config: config,
          pkName: 'id',
          pkValue: printMap.get(item),
          queryWrapper: queryWrapper,
          form: {},
          head: head,
          foot: [{
            name: '2',
            text: '制单人',
            col: 8,
            value: that.utils.getCurrentUser().name
          },
            {
              name: '3',
              text: '器械科',
              col: 8
            },
            {
              name: '1',
              text: '科室审核',
              value: flowModifier,
              col: 8
            },
          ]
        }
        paramArr.push(param)
      })
      global.utilPrint.openReport(paramArr)
    }
    if (key == 'printUser') { //打印
      let deptBySpArr = [];
      let printMap = new Map()
      let printPkArr = []


      let allData = await that.getAllData(config.code)
      let data = allData.data
      // data.filter((row) => !execDeptNameArr.includes(row.exec_dept_name) && execDeptNameArr.push(row.exec_dept_name))
      data.filter((row) => {
        let deptUser = {};
        deptUser.execDeptName = row.exec_dept_name
        deptUser.supplierName = row.supplier_name
        deptUser.inpatientNo = row.inpatient_no
        deptUser.patientName = row.patient_name
        deptUser.useDate = row.use_date
        deptUser.hrpKey = row.hrp_key
        deptUser.flowModifier = row.flow_modifier

        if (!JSON.stringify(deptBySpArr).includes(JSON.stringify(deptUser))) {
            printPkArr = [];
          deptBySpArr.push(deptUser)
          printMap.set(deptUser,printPkArr)
        }
        if (deptBySpArr.length >0 && JSON.stringify(deptBySpArr).includes(JSON.stringify(deptUser))) {
          printPkArr.push(row.id)
        }
      })
      let paramArr = []
      deptBySpArr.forEach((item,index) => {
        let execDeptName = item.execDeptName
        let inpatientNo = item.inpatientNo
        let supplierName = item.supplierName
        let patientName = item.patientName
        let useDate = item.useDate
        let hrpKey = item.hrpKey
        let flowModifier = item.flowModifier
        let queryWrapper = that.getQueryWrapper()
        queryWrapper.eq('exec_dept_name', execDeptName).eq('hrp_key', hrpKey)
          .eq('supplier_name', supplierName)
          .eq('inpatient_no', inpatientNo).eq('patient_name', patientName).eq("use_date", useDate)

        let head = [{
            name: 'execDeptName',
            text: '执行科室',
            value: execDeptName,
            col: 12
          },
          {
            name: 'supplierName',
            text: '供应商',
            value: supplierName,
            col: 12
          },
          {
            name: 'inpatientNo',
            text: '住院编号',
            value: inpatientNo,
            col: 8
          },
          {
            name: 'useDate',
            text: '消耗日期',
            value: useDate,
            col: 7
          },
          {
            name: 'hrpKey',
            text: 'HRP专购品单号',
            value: hrpKey,
            col: 8
          },
          {
            name: 'patientName',
            text: '病人姓名',
            value: patientName,
            col: 8
          },

        ]
        // that.cpUserQuery.forEach(item => {
        //   if (that.strings.isBlank(item.value)) {
        //     return true
        //   }
        //   let field = item.field
        //   if (item.queryType === 'numberArea' || item.queryType === 'dateArea') {
        //     head.push({name: item.field, text: item.label,value: item.value+'--'+ item.value1})
        //   } else  {
        //     if (item.field != 'exec_dept_name') {
        //       head.push({name: item.field, text: item.label,value: item.value})
        //     }
        //   }
        // })
        let param = {
          title: global.settings.hospitalName+'科室病人消耗单',
          subtitle: "",
          config: config,
          tableName: 'report_temp_dept_bill_hrp',
          pkName: 'id',
          pkValue: printMap.get(item),
          queryWrapper: queryWrapper,
          form: {},
          head: head,
          foot: [{
              name: '2',
              text: '制单人',
              col: 8,
              value: that.utils.getCurrentUser().name
            },
            {
              name: '3',
              text: '器械科',
              col: 8
            },
            {
              name: '1',
              text: '科室审核',
              value: flowModifier,
              col: 8
            },
          ]
        }
        paramArr.push(param)
      })
      console.log('分单后单据数量',paramArr)
      global.utilPrint.openReport(paramArr)
    }
  },
  //科室成本消耗明细
  async afterDeptBill(key, config, data, that) {
    // 分科室
    if (key == 'printDept') { //打印
      let execDeptNameArr = [];
      let allData = await that.getAllData(config.code)
      let data = allData.data
      data.filter((row) => !execDeptNameArr.includes(row.exec_dept_name) && execDeptNameArr.push(row.exec_dept_name))


      let paramArr = []
      execDeptNameArr.forEach(execDeptName => {
        let queryWrapper = that.getQueryWrapper()
        queryWrapper.eq('exec_dept_name', execDeptName)

        let head = [{
            name: 'execDeptName',
            text: '执行科室',
            value: execDeptName,
            col: 6
          },
          {
            name: 'deptName',
            text: '开立科室',
            value: execDeptName,
            col: 6
          }
        ]
        that.cpUserQuery.forEach(item => {
          if (that.strings.isBlank(item.value)) {
            return true
          }
          let field = item.field
          if (item.queryType === 'numberArea' || item.queryType === 'dateArea') {
            head.push({
              name: item.field,
              text: item.label,
              value: item.value + '--' + item.value1
            })
          } else {
            if (item.field != 'exec_dept_name') {
              head.push({
                name: item.field,
                text: item.label,
                value: item.value
              })
            }
          }
        })

        let param = {
          title: that.chartModel.config.name + '(' + execDeptName + ')',
          subtitle: that.chartModel.config.subtitle,
          config: config,
          queryWrapper: queryWrapper,
          form: {},
          head: head,
          foot: [{
              name: '2',
              text: '制单人',
              col: 8,
              value: that.utils.getCurrentUser().name
            },
            {
              name: '3',
              text: '器械科',
              col: 8
            },
            {
              name: '1',
              text: '科室审核',
              col: 8
            }
          ]
        }
        paramArr.push(param)
      })
      global.utilPrint.openReport(paramArr)
    }
    // 分供应商
    if (key == 'printSp') { //打印
      let arrSupplierName = [];
      let allData = await that.getAllData(config.code)
      let data = allData.data
      data.filter((row) => !arrSupplierName.includes(row.supplier_name) && arrSupplierName.push(row.supplier_name))


      let paramArr = []
      arrSupplierName.forEach(supplierName => {
        let queryWrapper = that.getQueryWrapper()
        queryWrapper.eq('supplier_name', supplierName)

        let head = [{
          name: 'supplierName',
          text: '供应商',
          value: supplierName,
          col: 6
        }, ]
        that.cpUserQuery.forEach(item => {
          if (that.strings.isBlank(item.value)) {
            return true
          }
          let field = item.field
          if (item.queryType === 'numberArea' || item.queryType === 'dateArea') {
            head.push({
              name: item.field,
              text: item.label,
              value: item.value + '--' + item.value1
            })
          } else {
            if (item.field != 'supplierName') {
              head.push({
                name: item.field,
                text: item.label,
                value: item.value
              })
            }
          }
        })

        let param = {
          title: that.chartModel.config.name + '(' + supplierName + ')',
          subtitle: that.chartModel.config.subtitle,
          config: config,
          queryWrapper: queryWrapper,
          form: {},
          head: head,
          foot: [{
              name: '3',
              text: '制单人',
              col: 8,
              value: that.utils.getCurrentUser().name
            },
            {
              name: '4',
              text: '供应商',
              col: 8
            },
            {
              name: '1',
              text: '器械科',
              col: 8
            },
            {
              name: '2',
              text: '科室审核',
              col: 8
            }
          ]
        }
        paramArr.push(param)
      })
      global.utilPrint.openReport(paramArr)
    }
  },
  //结算单明细
  async afterSettlementDtl(key, config, data, that) {

    // 分供应商
    if (key == 'printSp') { //打印
      let periodBySpArr = [];
      let allData = await that.getAllData(config.code)
      let data = allData.data
      // data.filter((row) => !execDeptNameArr.includes(row.exec_dept_name) && execDeptNameArr.push(row.exec_dept_name))
      data.filter((row) => {
        let periodBySp = {};
        periodBySp.period = row.period
        periodBySp.supplierName = row.supplier_name
        if (!JSON.stringify(periodBySpArr).includes(JSON.stringify(periodBySp))) {
          periodBySpArr.push(periodBySp)
        }
      })

      let paramArr = []
      periodBySpArr.forEach(item => {
        let period = item.period
        let supplierName = item.supplierName
        let queryWrapper = that.getQueryWrapper()
        queryWrapper.eq('supplier_name', supplierName).eq('period', period)

        let head = [{
            name: 'period',
            text: '会计期间',
            value: period,
            col: 5
          },
          {
            name: 'supplierName',
            text: '供应商',
            value: supplierName,
            col: 7
          },
        ]
        that.cpUserQuery.forEach(item => {
          if (that.strings.isBlank(item.value)) {
            return true
          }
          let field = item.field
          if (item.queryType === 'numberArea' || item.queryType === 'dateArea') {
            head.push({
              name: item.field,
              text: item.label,
              value: item.value + '--' + item.value1
            })
          } else {
            if (item.field != 'supplierName') {
              head.push({
                name: item.field,
                text: item.label,
                value: item.value
              })
            }
          }
        })
        // periodBySpArr[0].
        let param = {
          title: that.chartModel.config.name + '(' + supplierName + ')',
          subtitle: that.chartModel.config.subtitle,
          config: config,
          queryWrapper: queryWrapper,
          form: {},
          head: head,
          foot: [{
              name: '3',
              text: '制单人',
              col: 8,
              value: that.utils.getCurrentUser().name
            },
            {
              name: '4',
              text: '供应商',
              col: 8
            },
            {
              name: '1',
              text: '器械科',
              col: 8
            }
          ]
        }
        paramArr.push(param)
      })
      global.utilPrint.openReport(paramArr)
    }
  },
  //开立科室结算汇总
  async afterSettlementSumDeptCategory(key, config, data, that) {
    // 分供应商
    if (key == 'printDtl') { //打印
      let queryWrapper = that.getQueryWrapper()

      let head = [{name: 'field1', text: '编制单位', value: global.settings.hospitalName,col: 12}]
      that.cpUserQuery.forEach(item => {
        if (that.strings.isBlank(item.value)) {
          return true
        }
        let field = item.field
        if (item.queryType === 'numberArea' || item.queryType === 'dateArea') {
          head.push({name: item.field, text: item.label, value: item.value + '--' + item.value1})
        } else if (item.queryType === 'dataviewmore') {
          // queryWrapper.in(field, item.value)
        } else {
          head.push({name: item.field, text: item.label , value: item.value})
        }
      })

      let param = {
        title: that.chartModel.config.name,
        subtitle: that.chartModel.config.subtitle,
        config: that.chartModel.config,
        queryWrapper: queryWrapper,
        form: {},
        head: head,
        foot: [
          {name: '1', text: 'SPD项目负责人', col: 8},
          {name: '2', text: '药械仓库保管员', col: 8},
          {name: '3', text: '制表人', col: 8, value: that.utils.getCurrentUser().name},
        ]
      }
      global.utilPrint.openReport([param])
    }
  },
  //库存数量差异-按耗材
  async afterStockDiffMatr(key, config, data, that) {
    // 分供应商
    if (key == 'stockCheck') { //库存盘点
      global.utils.openMenu('check', {})
    }
  },
}

export default reportAfterBtnFuns
